package com.main.usermanagement.interfaces.assembler;

import com.main.common.base.base.BaseAssembler;
import com.main.common.base.base.BaseAssemblerFactory;
import com.main.usermanagement.domain.user.aggregate.User;
import com.main.usermanagement.domain.user.entity.UserAddress;
import com.main.usermanagement.domain.user.entity.UserInfo;
import com.main.usermanagement.domain.user.valueobject.Email;
import com.main.usermanagement.interfaces.dto.RegisterDTO;
import jakarta.validation.constraints.NotNull;

public class RegisterAssembler{
    private final BaseAssembler<User, RegisterDTO> baseAssembler;
    private final BaseAssembler<UserInfo,RegisterDTO> baseUserInfoAssembler;
    private final BaseAssembler<UserAddress,RegisterDTO> baseUserAddressAssembler;

    public RegisterAssembler(@NotNull BaseAssemblerFactory baseAssemblerFactory) {
        this.baseAssembler = baseAssemblerFactory.getService(RegisterDTO.class);
        this.baseUserInfoAssembler = baseAssemblerFactory.getService(RegisterDTO.class);
        this.baseUserAddressAssembler= baseAssemblerFactory.getService(RegisterDTO.class);
    }

    public User toEntity(RegisterDTO registerDTO) {
        User user = new User();
        user = baseAssembler.toEntity(registerDTO,user);
        user.setName(registerDTO.getName());
        user.setPhone(registerDTO.getPhone());
        user.setPhoneCode(registerDTO.getPhoneCode());
        Email email = new Email(registerDTO.getEmail().getValue());
        user.setEmail(email);
        user.setWechat(registerDTO.getWechat());
        user.setDirectSupervisorId(registerDTO.getDirectSupervisorId());
        user.setOrganizationAtomicId(registerDTO.getOrganizationAtomicId());
        UserInfo userInfo = new UserInfo();
        userInfo = baseUserInfoAssembler.toEntity(registerDTO,userInfo);
        userInfo.setGender(registerDTO.getGender());
        userInfo.setBirthDate(registerDTO.getBirthDate());
        userInfo.setIdCard(registerDTO.getIdCard());
        UserAddress userAddress = new UserAddress();
        userAddress = baseUserAddressAssembler.toEntity(registerDTO,userAddress);
        userAddress.setStreet(registerDTO.getAddress().getStreet());
        userAddress.setCity(registerDTO.getAddress().getCity());
        userAddress.setState(registerDTO.getAddress().getState());
        userAddress.setZip(registerDTO.getAddress().getZip());
        userAddress.setCountry(registerDTO.getAddress().getCountry());
        userInfo.setAddress(userAddress);
        user.setUserInfo(userInfo);
        return user;
    }

}
